<?php
/**
 *
 * 获取热门文章列表
 *
 *
 */
function get_hot_article_list($request)
{
	global $dsql;

	$limit = $request['limit'];
	$offset = $request['offset'];

	$artlist = array();

	$sql = "select
				count(*)
			from `#@__archives` 
			where 
				channel=1 
				and arcrank = 0 
			order by id desc";
	
	$total_count = $dsql->GetOne($sql);
	$total_count = $total_count['count(*)'];
		
	if($total_count > 0)
	{
		$sql = "select
					id,typeid,typeid2,litpic,
					title,shorttitle,description,writer,
					pubdate,click
				from `#@__archives` 
				where 
					channel=1 
					and arcrank = 0 
				order by id desc 
				limit $offset,$limit";
	
		$dsql->Execute('me', $sql);
		while($row = $dsql->GetArray())
		{
			$artlist[] = $row;
		}
	}

	$result['total_count'] = $total_count;
	$result['list'] = $artlist;

	return $result;
}

/**
 *
 * 存在二级分类，返回二级分类列表
 * 否则返回分类文章列表
 */
function get_article_type_list($request)
{
	global $dsql;

	$type_id = $request['type_id'];
	$limit = $request['limit'];
	$offset = $request['offset'];

	$sql = "select
				id,sortrank,typename
			from `#@__arctype` 
			where 
				reid='$type_id'
				and ishidden='0'
			order by sortrank asc";

	$dsql->SetQuery($sql);
	$dsql->Execute();

	//$row_count = $dsql->GetTotalRow($rsid="me");
	
	$list = array();

	while($row=$dsql->GetArray())
	{
		$list[] = $row;
	}

	return $list;
}

/**
 *
 * 获取最新文章列表
 */
function get_article_list($request)
{
	global $dsql;

	$type_id = $request['type_id'];
	$limit = $request['limit'];
	$offset = $request['offset'];

	$artlist = array();

	$sql = "select
				count(*)
			from `#@__archives` 
			where 
				typeid='$type_id'
				and channel=1 
				and arcrank=0 
			order by id desc";
	
	$total_count = $dsql->GetOne($sql);
	$total_count = $total_count['count(*)'];
	
	if($total_count > 0)
	{
		$sql = "select
					id,typeid,typeid2,litpic,
					title,shorttitle,description,writer,
					pubdate,click
				from `#@__archives` 
				where 
					typeid='$type_id'
					and channel=1 
					and arcrank=0 
				order by id desc 
				limit $offset,$limit";
	
		$dsql->Execute('me', $sql);
		while($row=$dsql->GetArray())
		{
			$artlist[] = $row;
		}
	}
	
	$result['total_count'] = $total_count;
	$result['list'] = array_values($artlist);

	return $result;
}

function search_article_list($request)
{
	global $dsql;

	$keyword = '%'.strtolower($request['keyword']).'%';
	$limit = $request['limit'];
	$offset = $request['offset'];

	$artlist = array();
	
	$sql = "select
				count(*)
			from `#@__archives` arc
				left join `#@__arctype` tp on tp.id=arc.typeid
				left join `#@__addonarticle` addon on addon.aid=arc.id
			where 
				(arc.title like '$keyword' or addon.body like '$keyword')
				and arc.channel=1 
				and arc.arcrank=0 
			order by arc.id desc ";
	
	$total_count = $dsql->GetOne($sql);
	$total_count = $total_count['count(*)'];
	
	if($total_count > 0)
	{
		$sql = "select
					arc.id,arc.typeid,tp.typename,arc.title,
					arc.litpic,arc.source,arc.description,
					arc.pubdate,arc.writer,arc.click
				from `#@__archives` arc
					left join `#@__arctype` tp on tp.id=arc.typeid
					left join `#@__addonarticle` addon on addon.aid=arc.id
				where 
					(arc.title like '$keyword' or addon.body like '$keyword')
					and arc.channel=1 
					and arc.arcrank=0 
				order by arc.id desc 
				limit $offset,$limit";
	
		$dsql->SetQuery($sql);
		$dsql->Execute();
		while($row=$dsql->GetArray())
		{
			$artlist[] = $row;
		}
	}

	$result['total_count'] = $total_count;
	$result['list'] = $artlist;

	return $result;
}

function get_article_with_pic_list($request)
{
	global $dsql;

	$type_id = $request['type_id'];
	$limit = $request['limit'];
	$offset = $request['offset'];

	$artlist = array();
	
	$type_condition = $type_id ? "and typeid='$type_id'" : '';
	
	$sql = "select
				count(*)
			from `#@__archives` 
			where 
				litpic <> ''
				$type_condition
				and channel=1 
				and arcrank=0 
			order by id desc ";
	
	$total_count = $dsql->GetOne($sql);
	$total_count = isset($total_count['count(*)']) ? $total_count['count(*)'] : '';

	if($total_count > 0)
	{
		$sql = "select
					id,litpic,title,pubdate
				from `#@__archives` 
				where 
					litpic <> ''
					$type_condition
					and channel=1 
					and arcrank=0 
				order by id desc 
				limit $offset,$limit";
	
		$dsql->SetQuery($sql);
		$dsql->Execute();
		while($row=$dsql->GetArray())
		{
			$artlist[] = $row;
		}
	}

	$result['total_count'] = $total_count;
	$result['list'] = $artlist;

	return $result;
}

/**
 *
 * 文章详情
 * @param $id，文章ID
 *
 */
function get_article_info($id)
{
	global $dsql;

	//文档信息
	$query = "
	Select
		arc.id,arc.typeid,tp.typename,arc.title,
		arc.litpic,arc.source,
		arc.pubdate,arc.writer,arc.click,
		addon.body,arc.notpost
	from `#@__archives` arc
		left join `#@__arctype` tp on tp.id=arc.typeid
		left join `#@__addonarticle` addon on addon.aid=arc.id
	where
		arc.id='$id'";
	$row = $dsql->GetOne($query, MYSQL_ASSOC);

	return $row;
}

function get_feedback_count($aid)
{
	global $dsql;
	
	$count = $dsql->GetOne("SELECT count(*) from `#@__feedback` where aid='$aid' AND ischeck='1'");
	
	return $count['count(*)'];
}

function get_feedback_list($request)
{
	global $dsql;
	
	$aid = $request['aid'];
	$ftype = $request['type'];
	
	$limit = $request['limit'];
	$offset = $request['offset'];
	
	if(empty($ftype) || ($ftype!='good' && $ftype!='bad' && $ftype!='feedback'))
    {
        $ftype = '';
    }
    
    $list = array();
    
    $wquery = $ftype!='' ? " And ftype like '$ftype' " : '';

    $sql = "select
				count(*)
			FROM `#@__feedback` fb
                 WHERE fb.aid='$aid' AND fb.ischeck='1' $wquery 
                 ORDER BY fb.id desc";
	
	$total_count = $dsql->GetOne($sql);
	$total_count = $total_count['count(*)'];

	if($total_count > 0)
	{
	    //评论内容列表
	    $sql = "SELECT 
	    			fb.id,fb.aid,fb.dtime,fb.bad,
	    			fb.good,fb.ftype,fb.face,fb.msg
	    			FROM `#@__feedback` fb
	                 WHERE fb.aid='$aid' AND fb.ischeck='1' $wquery 
	                 ORDER BY fb.id desc
	                 limit $offset,$limit";
	    
	    $dsql->SetQuery($sql);
		$dsql->Execute();
		while($row=$dsql->GetArray())
		{
			$list[] = $row;
		}
	}
	
	$result['total_count'] = $total_count;
	$result['list'] = $list;

	return $result;
}

function get_feedback_info($id)
{
	global $dsql;
	
	$row = $dsql->GetOne("SELECT id,aid,dtime,bad,
    			good,ftype,face,msg FROM `#@__feedback` WHERE id ='$id'");
	
	return $row;
}

function add_feedback($request)
{
	global $dsql, $cfg_feedback_add, $cfg_feedback_sub;

	$aid = $request['aid'];
	$dtime = $request['dtime'];
	
	$inquery = "INSERT INTO `#@__feedback`
					(`aid`,`typeid`,`username`,`arctitle`,`ip`,`ischeck`,`dtime`, `mid`,`bad`,`good`,`ftype`,`face`,`msg`)
                VALUES
                	('$aid','{$request['type_id']}','{$request['username']}','{$request['arctitle']}',
                	'{$request['ip']}','{$request['ischeck']}','$dtime', '0',
                	'0','0','{$request['ftype']}','{$request['face']}','{$request['msg']}'); ";
	$rs = $dsql->ExecuteNoneQuery($inquery);
	
	if($request['ftype']=='bad')
    {
        $dsql->ExecuteNoneQuery("UPDATE `#@__archives` SET scores=scores-{$cfg_feedback_sub},badpost=badpost+1,lastpost='$dtime' WHERE id='$aid' ");
    }
    else if($request['ftype']=='good')
    {
        $dsql->ExecuteNoneQuery("UPDATE `#@__archives` SET scores=scores+{$cfg_feedback_add},goodpost=goodpost+1,lastpost='$dtime' WHERE id='$aid' ");
    }
    else
    {
        $dsql->ExecuteNoneQuery("UPDATE `#@__archives` SET scores=scores+1,lastpost='$dtime' WHERE id='$aid' ");
    }
	
	return $rs;
}

function add_bad_good_count($id, $type)
{
	global $dsql;
	
	$count = '0';
	
	if($type=='good')
	{
		$dsql->ExecuteNoneQuery("UPDATE `#@__feedback` SET good = good+1 WHERE id='$id' ");
		$count = $dsql->GetOne("SELECT good FROM `#@__feedback` WHERE id='$id' ");
	}
	else if($type=='bad')
	{
		$dsql->ExecuteNoneQuery("UPDATE `#@__feedback` SET bad = bad+1 WHERE id='$id' ");
		$count = $dsql->GetOne("SELECT bad FROM `#@__feedback` WHERE id='$id' ");
	}
	
	return $count;
}